<template>
  <div class="searchpsw">
    <van-nav-bar
      title="找回密码第一步"
      left-arrow
      @click-left="onClickLeft"
    />

    <div
      v-if="isFirstStep"
      class="first-step"
    >
      <div class="progress-bar">
        <img
          src="@/assets/images/flow1.png"
          alt=""
        />
      </div>
      <van-form @submit="onSubmit">
        <van-field
          label-width="50px"
          class="username"
          v-model="username"
          name="用户名"
          placeholder="请输入手机号"
          label="用户名"
          :rules="[{ required: true, message: '用户名不能为空' }]"
          @blur="check"
        >
        </van-field>
        <van-field
          class="yzm"
          v-model="sms"
          center
          clearable
          label-width="70px"
          label="短信验证码"
          placeholder="请输入短信验证码"
          :rules="[
            { required: true, message: '验证码不能为空' },
            {
              validator: inspectphone,
              message: '请输入正确的手机号码',
            },
          ]"
        >
          <template #button>
            <van-button
              size="small"
              type="primary"
              :disabled="flag"
              @click.prevent="getyzm"
              >发送验证码</van-button
            >
          </template>
        </van-field>

        <div>
          <van-button
            class="f-btn"
            round
            block
            type="info"
            native-type="submit"
            @click.prevent="nextStep"
            >下一步</van-button
          >
        </div>
      </van-form>
    </div>
    <div
      v-else
      class="second-step"
    >
      <div class="progress-bar">
        <img
          src="@/assets/images/flow2.png"
          alt=""
        />
      </div>
      <van-form @submit="onSubmit">
        <van-field
          label-width="50px"
          class="username"
          v-model="password1"
          name="密码"
          placeholder="请输入新的密码"
          label="密码"
          :rules="[
            { required: true, message: '密码不能为空' },
            { pattern: checkpsw, message: '请输入正确内容' },
          ]"
        >
        </van-field>
        <van-field
          label-width="60px"
          class="username"
          v-model="password2"
          name="二次密码"
          placeholder="确认密码"
          label="确认密码"
          :rules="[
            { required: true, message: '密码不能为空' },
            { pattern: checkpsw, message: '请输入正确内容' },
            { validator, message: '两次密码必须一致' },
          ]"
        >
        </van-field>

        <div>
          <van-button
            class="f-btn"
            round
            block
            type="info"
            native-type="submit"
            >完成</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>
<script>
import { Toast } from 'vant';
import { updatePwd } from '@/api';
export default {
  data() {
    return {
      username: '',
      sms: '',
      password1: '',
      password2: '',
      checkUserName: /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
      checkpsw: /^\w{6,}$/,
      isFirstStep: true,
      flag: true,
    };
  },
  methods: {
    check() {
      if (this.checkUserName.test(this.username)) {
        this.flag = false;
      }
    },
    inspectphone() {
      return this.checkUserName.test(this.username);
    },
    validator() {
      return this.password1 === this.password2;
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    getyzm() {
      var num = '';
      for (var i = 0; i < 6; i++) {
        num += Math.floor(Math.random() * 10);
      }
      localStorage.yzm = JSON.stringify(num);
      Toast('验证码已经发送');
    },
    nextStep() {
      const yzm = JSON.parse(localStorage.yzm || 'null');
      if (yzm == this.sms) {
        this.isFirstStep = false;
      } else {
        Toast('验证码错误');
      }
    },
    async onSubmit() {
      const person = {
        user_pwd: this.password1,
      };
      const res = await updatePwd(person, this.$store.state.user.user.id);
      const {
        data: { code },
      } = res;
      console.log(res);

      if (code == 0) {
        Toast('成功修改密码');
        this.$router.push('/login');
      }
    },
  },
};
</script>
<style lang="scss">
body {
  background: #fafafa;
}
.searchpsw {
  .van-nav-bar__content {
    height: 45px;
    .van-nav-bar__left {
      .van-icon {
        color: #323233;
        font-size: 22px;
      }
    }
    .van-nav-bar__title {
      font-size: 18px;
    }
  }
  .progress-bar {
    width: 100%;
    margin: 15px auto;
    img {
      width: 100%;
    }
  }
  .first-step,
  .second-step {
    width: 84%;
    padding: 20px 8% 0 8%;

    .username {
      margin-bottom: 15px;
      border-radius: 3px;
    }

    .yzm {
      margin-bottom: 15px;
      border-radius: 3px;
      height: 44px;

      .van-button {
        width: 78px;
      }
      .van-button--primary {
        color: #2577e3;
        background: none;
        border: none;
      }
    }

    .f-btn {
      margin-bottom: 15px;
      font-size: 15px;
      letter-spacing: 10px;
    }
  }
}
</style>
